package com.cn.sys.main.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cn.sys.frame.dao.BaseSpringJdbcDao;
import com.cn.sys.frame.exception.ServiceException;
import com.cn.sys.frame.service.impl.BaseServiceImpl;
import com.cn.sys.frame.util.page.PageData;
import com.cn.sys.frame.util.query.QueryCondition;
import com.cn.sys.main.annotation.OperationDescription;
import com.cn.sys.main.dao.SysOrganDao;
import com.cn.sys.main.entity.SysOrgan;
import com.cn.sys.main.service.SysOrganService;

@Service("sysOrganService")
public class SysOrganServiceImpl extends BaseServiceImpl<SysOrgan, Long>
		implements SysOrganService {
	private SysOrganDao sysOrganDao;
	
	@Autowired
	public void setBaseDao(BaseSpringJdbcDao<SysOrgan, Long> sysOrganDao) {
		this.baseDao = sysOrganDao;
		this.sysOrganDao = (SysOrganDao) sysOrganDao;
	}

	@Cacheable(value="sysOrganCache", key=" #id + 'get'")
	public SysOrgan get(Long id) throws Exception {
		return super.get(id);
	}

	@OperationDescription(operateType = "save", 
			content = "添加组织机构信息\nID:${#entity.id}\n机构名称:${#entity.name}\n机构代码:${#entity.code}\n" +
					"父机构:${#entity.parentId}\n联系人:${#entity.linkman}\n创建时间:${#entity.createTime}", 
			operateObj = "SysOrgan")
	public SysOrgan save(SysOrgan entity) throws Exception {
		return super.save(entity);
	}

	@OperationDescription(operateType = "update", 
			content = "添加组织机构信息\nID:${#entity.id}\n机构名称:${#entity.name}\n机构代码:${#entity.code}\n" +
					"父机构:${#entity.parentId}\n联系人:${#entity.linkman}\n修改时间:${#entity.updateTime}", 
			operateObj = "SysOrgan")
	@CacheEvict(value="sysOrganCache", key=" #entity.id + 'get'")
	public SysOrgan update(SysOrgan entity) throws Exception {
		return super.update(entity);
	}

	@Override
	public long getCount(QueryCondition queryCondition) throws Exception {
		return super.getCount(queryCondition);
	}

	@Override
	public List<SysOrgan> getAll() throws Exception {
		return super.getAll();
	}

	@Override
	public PageData<SysOrgan> getPageData(PageData<SysOrgan> pageData,
			QueryCondition queryCondition) throws Exception {
		return super.getPageData(pageData, queryCondition);
	}

	public List<Map<String, Object>> getOrganTreeList() throws Exception{
		String sql = "SELECT ID \"ID\", NAME \"NAME\", PARENT_ID \"PARENT_ID\" FROM SYS_ORGAN";
		List<Map<String, Object>> list = sysOrganDao.getJdbcTemplate().queryForList(sql);
		return list;
	}

	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class, readOnly=false)
	@OperationDescription(operateType = "delete", content = "删除组织机构信息\nID:${#id}", operateObj = "SysOrgan")
	@CacheEvict(value="sysOrganCache", key=" #id + 'get'")
	public int delete(Long id) throws Exception {
		int result = 0;
		try {
			sysOrganDao.getJdbcTemplate().update("UPDATE SYS_USER SET ORGAN_ID = ? WHERE ORGAN_ID = ?", new Object[]{null, id});
			result = sysOrganDao.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException("删除组织机构失败!["+e.getMessage()+"]");
		}
		return result;
	}

	public List<SysOrgan> getOrgansByParentId(String pId) throws Exception {
		List<SysOrgan> list = sysOrganDao.listByCondition("AND PARENT_ID = ?", Long.valueOf(pId));
		return list;
	}

}
